package com.headStorm.concurrent.map;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangqq
 * @create 2021-07-21 17:03
 **/
public class MapUseDemo {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        ConcurrentHashMap<String, Integer> concurrentHashMap = new ConcurrentHashMap();
        //concurrentHashMap.put("a", "a");
        //concurrentHashMap.put("a", "cccc");


//        Object a = concurrentHashMap.computeIfAbsent("a", key -> {
//            //System.out.println(key);
//            return "java";
//        });
//        System.out.println(a + " " + concurrentHashMap.get("a"));
//        Object o = concurrentHashMap.putIfAbsent("a", "c");
//        System.out.println(o);
//        System.out.println(concurrentHashMap.get("a"));
        System.out.println(concurrentHashMap.computeIfAbsent("a", key -> 0));
        System.out.println(concurrentHashMap.computeIfPresent("a", (key, value) -> new AtomicInteger(value).incrementAndGet()));
        System.out.println(concurrentHashMap.computeIfPresent("a", (key, value) -> new AtomicInteger(value).incrementAndGet()));
        System.out.println(concurrentHashMap.computeIfPresent("a", (key, value) -> new AtomicInteger(value).incrementAndGet()));
        System.out.println(concurrentHashMap.get("a"));
        //System.out.println(concurrentHashMap.computeIfAbsent("a", s -> s + "不存在,新增"));
    }
}
